package com.fr.report.core.cal;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.file.CacheManager;
import com.fr.base.file.DatasourceManager;
import com.fr.data.TableData;
import com.fr.data.impl.DBTableData;
import com.fr.data.impl.TableDataAdapter;
import com.fr.report.TableDataSource;
import com.fr.report.TemplateReport;
import com.fr.report.TemplateWorkBook;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Calculator;
import com.fr.report.script.core.parser.UtilEvalError;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class SheetExecuter
{
  protected Calculator calculator;
  protected Map parameterMap;

  protected TableData __us_name2TableData(String paramString)
  {
    Object[] arrayOfObject = dealwithDsName(paramString);
    if (arrayOfObject[1] != null)
      return ((TableData)arrayOfObject[1]);
    paramString = (String)arrayOfObject[0];
    return __s_name2TableData(paramString);
  }

  protected TableData __s_name2TableData(String paramString)
  {
    TableDataSource localTableDataSource = this.calculator.getCurrentTableDataSource();
    TableData localTableData = localTableDataSource.getTableData(paramString);
    if (localTableData != null)
      __setParameterValues(localTableData.getParameters());
    if (localTableData == null)
    {
      DatasourceManager localDatasourceManager = FRContext.getDatasourceManager();
      localTableData = localDatasourceManager.getTableData(paramString);
      if (localTableData == null)
      {
        localTableData = TableData.EMPTY_TABLEDATA;
        FRContext.getLogger().log(Level.INFO, paramString + ":" + Inter.getLocText("Utils-Can_not_find_the_corresponding_TableData"));
      }
      localTableDataSource.putTableData(paramString, localTableData);
      __setParameterValues(localTableData.getParameters());
    }
    return localTableData;
  }

  protected Object[] dealwithDsName(String paramString)
  {
    Object[] arrayOfObject = new Object[2];
    if (paramString.startsWith("="))
      try
      {
        Object localObject = this.calculator.eval(paramString.substring(1));
        if (localObject == null)
          arrayOfObject[1] = TableData.EMPTY_TABLEDATA;
        else if (localObject instanceof TableData)
          arrayOfObject[1] = localObject;
        else
          paramString = localObject.toString();
      }
      catch (UtilEvalError localUtilEvalError)
      {
        String str = "error happens at resovle TableData\nformula expression is " + paramString + "\nerror message is " + localUtilEvalError.getMessage();
        FRContext.getLogger().log(Level.WARNING, str, localUtilEvalError);
      }
    arrayOfObject[0] = paramString;
    return arrayOfObject;
  }

  protected void __setParameterValues(Parameter[] paramArrayOfParameter)
  {
    if (paramArrayOfParameter == null)
      return;
    for (int i = 0; i < paramArrayOfParameter.length; ++i)
    {
      Parameter localParameter = paramArrayOfParameter[i];
      Object localObject = this.parameterMap.get(localParameter.getName());
      if (localObject != null)
        localParameter.setValue(localObject);
    }
  }

  protected void releaseTableData(TemplateReport paramTemplateReport)
  {
    String str;
    TableData localTableData;
    Iterator localIterator = paramTemplateReport.getTemplateWorkBook().getTableDataNameIterator();
    do
    {
      if (!(localIterator.hasNext()))
        return;
      str = (String)localIterator.next();
      localTableData = paramTemplateReport.getTemplateWorkBook().getTableData(str);
    }
    while (str == null);
    try
    {
      localTableData.release();
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
  }

  public static TableDataAdapter createDefaultDBTableDataAdapter(DBTableData paramDBTableData, String paramString)
  {
    TableDataAdapter localTableDataAdapter;
    if (paramDBTableData == null)
      return null;
    if (paramDBTableData.isShareTableData())
    {
      localTableDataAdapter = FRContext.getCacheManager().get(paramDBTableData.getDatabase(), paramString, paramDBTableData.getMaxMemRowCount());
      localTableDataAdapter.register(paramDBTableData);
    }
    else
    {
      localTableDataAdapter = DBTableData.createCacheableAdapter(paramDBTableData.getDatabase(), paramString, paramDBTableData.getResultIndexOfOut(), paramDBTableData.getMaxMemRowCount());
    }
    return localTableDataAdapter;
  }

  public TableDataAdapter createDBTableDataAdapter(DBTableData paramDBTableData, String paramString)
  {
    return createDefaultDBTableDataAdapter(paramDBTableData, paramString);
  }
}